<?php
/**
 * Created by PhpStorm
 * User: huangjiacheng
 * Date: 2023/6/5
 * Time: 11:18
 */

namespace app\command;

use think\facade\Db;
use think\console\input\Option;
use think\console\Command;
use think\facade\Console;
use think\console\Output;
use think\console\Input;


class RemoveTradeCode extends Command
{
    protected function configure()
    {
        $this->setName('remove:trade_code')
            ->addOption('table_name', null, Option::VALUE_REQUIRED, '输入表名字')
            ->addOption('id', null, Option::VALUE_REQUIRED, '输入表id数据')
            ->setDescription('移除trade_code信息');
    }

    protected function execute(Input $input, Output $output)
    {
        $output->info('这是移除trade_code信息');
        $table_name = $input->getOption('table_name');
        $id = $input->getOption('id');
        if ($table_name == 'trade_code') {
            $sql = "select * from $table_name where id= $id";
            $list = Db::query($sql);
            $row = $list[0];
            $yuer_jia = $row['buy'] * $row['buy_num'];
            $chichang_jian = $row['cur_price'] * $row['buy_num'];


            $sql = "select * from zj_info where id=1";
            $list = Db::query($sql);
            $row = $list[0];


            $yuer = $row['yuer'] +$yuer_jia;
            $chicang = $row['chicang'] - $chichang_jian;
            $shizhi = $yuer + $chicang;
            $yingkui = ($shizhi - $row['bj'])/$row['bj'];

            $update = "update zj_info set yuer='$yuer',chicang='$chicang',shizhi='$shizhi',yingkui='$yingkui' where id = 1";
            Db::query($update);

            $del = "delete from $table_name where id='$id'";
            Db::query($del);

        }elseif ($table_name == 'trade_two_code') {
            $sql = "select * from $table_name where id= $id";
            $list = Db::query($sql);
            $row = $list[0];
            $yuer_jia = $row['buy'] * $row['buy_num'];
            $chichang_jian = $row['cur_price'] * $row['buy_num'];

            $sql = "select * from zj_info where id=2";
            $list = Db::query($sql);
            $row = $list[0];


            $yuer = $row['yuer'] +$yuer_jia;
            $chicang = $row['chicang'] - $chichang_jian;
            $shizhi = $yuer + $chicang;
            $yingkui = ($shizhi - $row['bj'])/$row['bj'];

            $update = "update zj_info set yuer='$yuer',chicang='$chicang',shizhi='$shizhi',yingkui='$yingkui' where id = 2";
            Db::query($update);

            $del = "delete from $table_name where id='$id'";
            Db::query($del);

        }

    }
}